package com.example.shishaolong.warehousemanager;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.shishaolong.warehousemanager.common.SystemConst;
import com.example.shishaolong.warehousemanager.dao.AdminDao;
import com.example.shishaolong.warehousemanager.entity.AdminEntity;
import com.example.shishaolong.warehousemanager.view.TitleLayout;

import java.util.List;

public class AdminActivity extends AppCompatActivity {

    private AdminDao adminDao;
    private AdminAdapter adapter;
    private List<AdminEntity> adminEntityList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin);

        //隐藏标题栏
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.hide();
        }
        //设置标题栏
        TitleLayout titleLayout = (TitleLayout) findViewById(R.id.title_layout);
        titleLayout.setTitle("管理员列表");
        titleLayout.setRightButton(new TitleLayout.ButotonEntity("新增", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //启动管理员列表页面
                Intent AdminInfoIntent = new Intent();
                //传入新增参数  --新增
                AdminInfoIntent.putExtra(SystemConst.ADMIN_GOTO_FROM,SystemConst.ADMIN_GOTO_FROM_ADD);
                AdminInfoIntent.setClass(AdminActivity.this,AdminAddActivity.class);
//                startActivity(intent);
                startActivityForResult(AdminInfoIntent,SystemConst.ADMIN_INFO_ACTIVITY_START_FOR_RESULT);  //加页面跳转标识符
            }
        }));

        //初始化管理员的数据访问层实例
        adminDao = new AdminDao(getApplicationContext());
        //获取到管理员列表
        adminEntityList = adminDao.findAll();

        //构造Adapter
        adapter = new AdminAdapter(AdminActivity.this, adminEntityList);
        final ListView adminListView = (ListView) findViewById(R.id.admin_listView);
        adminListView.setAdapter(adapter);
        adminListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //勾选事件，点击选择记录
                boolean checked = adminEntityList.get(position).isChecked();
                if (!checked) {
                    adminEntityList.get(position).setChecked(true);
                }else {
                    adminEntityList.get(position).setChecked(false);
                }
                adapter.notifyDataSetChanged();
            }
        });

        //长按监听事件-进入修改界面
        adminListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                Intent updateAdminIntent = new Intent();
                //传入参数  --修改
                updateAdminIntent.putExtra(SystemConst.ADMIN_GOTO_FROM,SystemConst.ADMIN_GOTO_FROM_UPDATE);
                updateAdminIntent.putExtra(SystemConst.ADMIN_ENTITY_ID, adminEntityList.get(position).getId()); //将id传入
                updateAdminIntent.setClass(AdminActivity.this,AdminAddActivity.class);
                //跳转到修改页面，并设置了页面标识符（用于所跳转页面返回到当前页面，能判断出是哪个页面）
                startActivityForResult(updateAdminIntent,SystemConst.ADMIN_INFO_ACTIVITY_START_FOR_RESULT);   //启动修改页面

                return false;// return false，click还会被调用，true click不会再调用
            }
        });

        //全选按钮按钮设置
        Button all_sel = (Button) findViewById(R.id.admin_list_all_sel);
        Button all_unsel = (Button) findViewById(R.id.admin_list_all_unsel);
        Button batchDelete = (Button)findViewById(R.id.admin_list_batch_delete); //批量删除按钮
        all_sel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                for (int i = 0; i < adminEntityList.size(); i++) {
                    adminEntityList.get(i).setChecked(true);
                }

                adapter.notifyDataSetChanged();
            }
        });

        //全部取消的设置
        all_unsel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                for (int i = 0; i < adminEntityList.size(); i++) {
                    adminEntityList.get(i).setChecked(false);
                }
                adapter.notifyDataSetChanged();
            }
        });

        //批量删除的监听事件
        batchDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取到所有选中项
                int count = 0;
                for (int i = 0; i< adminEntityList.size(); i++) {
                    AdminEntity adminEntity = adminEntityList.get(i);
                    if(adminEntity.isChecked()){
                        adminDao.delete(adminEntity.getId());
                        //移除界面里列表中的已删除项
                        adminEntityList.remove(i);
                        count++;
                    }
                }
                //提示出库成功
                if(count>0){
                    Toast.makeText(AdminActivity.this.getApplicationContext(),"删除成功！",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(AdminActivity.this.getApplicationContext(),"请先选择。",Toast.LENGTH_SHORT).show();
                }
                //刷新界面
                adapter.notifyDataSetChanged();
            }
        });

    }

    /**
     * 从其他页面返回到此页面时调用，   用于刷新此页面的一些数据    数据刷新
     * @param requestCode
     * @param resultCode 返回码，对应上了，从此页面启动其他页面时设置的，回调时用于判断是从哪个页面返回的
     * @param data intent，其它页面可以设置一些数据，返回到这个页面时再取出来。
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

        switch (requestCode){ //判断是从哪个页面返回的
            case SystemConst.ADMIN_INFO_ACTIVITY_START_FOR_RESULT:

                adminEntityList.clear();
                adminEntityList.addAll(adminDao.findAll());
                adapter.notifyDataSetChanged();
                break;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }
}
